# RSS Fetch Test Script
# Simple version without emojis and special characters

$baseUrl = "http://localhost:8080"

Write-Host "=== Testing RSS Fetch Feature ===" -ForegroundColor Green

# Step 1: Get feed list
Write-Host "`nStep 1: Getting feed list..." -ForegroundColor Cyan
try {
    $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds" -Method GET
    $feeds = $response.data
    Write-Host "SUCCESS: Found $($feeds.Count) feeds" -ForegroundColor Green
    
    if ($feeds.Count -eq 0) {
        Write-Host "ERROR: No feeds found. Please add a feed first." -ForegroundColor Red
        exit 1
    }
    
    # Display feeds
    for ($i = 0; $i -lt $feeds.Count; $i++) {
        $feed = $feeds[$i]
        Write-Host "  $($i + 1). [ID: $($feed.id)] $($feed.title)" -ForegroundColor White
        Write-Host "     URL: $($feed.url)" -ForegroundColor Gray
    }
    
    # Use first feed for testing
    $testFeed = $feeds[0]
    $testFeedId = $testFeed.id
    Write-Host "`nWill test feed: $($testFeed.title)" -ForegroundColor Green
}
catch {
    Write-Host "ERROR: Failed to get feeds - $_" -ForegroundColor Red
    exit 1
}

# Step 2: Refresh feed (fetch articles)
Write-Host "`nStep 2: Refreshing feed (fetching articles)..." -ForegroundColor Cyan
try {
    Write-Host "Fetching from: $($testFeed.url)" -ForegroundColor White
    $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds/$testFeedId/refresh" -Method POST
    Write-Host "SUCCESS: $($response.message)" -ForegroundColor Green
    
    Write-Host "Waiting 5 seconds..." -ForegroundColor Gray
    Start-Sleep -Seconds 5
}
catch {
    Write-Host "ERROR: Failed to refresh feed - $_" -ForegroundColor Red
    if ($_.ErrorDetails.Message) {
        Write-Host "Details: $($_.ErrorDetails.Message)" -ForegroundColor Red
    }
    exit 1
}

# Step 3: Check articles
Write-Host "`nStep 3: Checking fetched articles..." -ForegroundColor Cyan
try {
    $url = "$baseUrl/api/articles?page=1&size=10"
    $response = Invoke-RestMethod -Uri $url -Method GET
    
    Write-Host "SUCCESS: Total articles: $($response.data.total)" -ForegroundColor Green
    Write-Host "SUCCESS: Current page articles: $($response.data.articles.Count)" -ForegroundColor Green
    
    if ($response.data.articles.Count -gt 0) {
        Write-Host "`nLatest articles:" -ForegroundColor Cyan
        for ($i = 0; $i -lt [Math]::Min(5, $response.data.articles.Count); $i++) {
            $article = $response.data.articles[$i]
            Write-Host "  $($i + 1). $($article.title)" -ForegroundColor White
            Write-Host "     Author: $($article.author) | Published: $($article.published_at)" -ForegroundColor Gray
        }
        
        $global:firstArticleCount = $response.data.total
    }
    else {
        Write-Host "WARNING: No articles fetched. Possible reasons:" -ForegroundColor Yellow
        Write-Host "  1. RSS feed has no content" -ForegroundColor Yellow
        Write-Host "  2. RSS feed format is incorrect" -ForegroundColor Yellow
        Write-Host "  3. Network connection issue" -ForegroundColor Yellow
    }
}
catch {
    Write-Host "ERROR: Failed to get articles - $_" -ForegroundColor Red
}

# Step 4: Test deduplication
Write-Host "`nStep 4: Testing deduplication (refresh again)..." -ForegroundColor Cyan
try {
    Write-Host "Fetching again from: $($testFeed.url)" -ForegroundColor White
    $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds/$testFeedId/refresh" -Method POST
    Write-Host "SUCCESS: $($response.message)" -ForegroundColor Green
    
    Start-Sleep -Seconds 3
    
    $url = "$baseUrl/api/articles?page=1&size=10"
    $newResponse = Invoke-RestMethod -Uri $url -Method GET
    
    Write-Host "Total articles after 2nd fetch: $($newResponse.data.total)" -ForegroundColor Green
    
    if ($newResponse.data.total -eq $global:firstArticleCount) {
        Write-Host "SUCCESS: Deduplication works! No duplicate articles added." -ForegroundColor Green
    }
    else {
        Write-Host "INFO: Article count changed. May have new articles published." -ForegroundColor Yellow
    }
}
catch {
    Write-Host "WARNING: Deduplication test failed - $_" -ForegroundColor Yellow
}

# Summary
Write-Host "`n=== Test Complete ===" -ForegroundColor Green
Write-Host "`nTips:" -ForegroundColor Yellow
Write-Host "  - Check server logs if no articles were fetched" -ForegroundColor Gray
Write-Host "  - Try adding other RSS feeds for testing" -ForegroundColor Gray
Write-Host "  - Recommended: Ruanyifeng blog https://www.ruanyifeng.com/blog/atom.xml" -ForegroundColor Gray
